package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.utils.PicassoUtil;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Description:添加图片显示
 * Created by lidongcai on 17/5/17.
 * Email:  lidongcaiocc@163.com
 */

public class OperationImageGridAdapter  extends RecyclerView.Adapter<OperationImageGridAdapter.ViewHolder>{

    private Context mContext;
    private ArrayList<String> imageUrls;
    private OnItemClickListener mOnItemClickListener;

    public OperationImageGridAdapter(Context mContext, ArrayList<String> imageUrls) {
        this.mContext = mContext;
        this.imageUrls = imageUrls;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_submit_lab_image, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (imageUrls.get(position).equals("default")){
            holder.photoImageView.setImageResource(R.mipmap.plus2);
            holder.delImageView.setVisibility(View.GONE);
        }else {
            holder.delImageView.setVisibility(View.VISIBLE);

            /**
             * 判断图片路径是网络地址还是本地图片
             * 设置路径之中包含“storage”的为本地图片
             */
//            if (imageUrls.get(position).contains("storage")) {
                File file = new File(imageUrls.get(position));
                //按比例扩大图片的size居中显示，使得图片长(宽)等于或大于View的长(宽)
                holder.photoImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                holder.photoImageView.setImageURI(Uri.fromFile(file));
                PicassoUtil.loadLocalImage(mContext,"file://"+file.getPath(),holder.photoImageView);
                //                holder.imageViewBig.setImageURI(Uri.parse(imageUrls.get(position)));
//            } else {
//                PicassoUtil.loadImage(mContext,"",R.mipmap.logo,holder.photoImageView);
//            }


        }


    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    public interface OnItemClickListener{
        void onPhotoClick(int position);
        void onDelClick(int position);
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView photoImageView,delImageView;

        public ViewHolder(View itemView) {
            super(itemView);

            photoImageView = (ImageView) itemView.findViewById(R.id.item_submit_lab_photo_iv);
            delImageView = (ImageView) itemView.findViewById(R.id.item_submit_lab_del_iv);

            photoImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onPhotoClick(getAdapterPosition());
                }
            });

            delImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onDelClick(getAdapterPosition());
                }
            });


        }
    }
}
